<?php
$ROOT = "../../../";
require_once ($ROOT . 'config/config.php');

class SubcategoryIndex{	
	private $db;
	private $post;
	private $smarty;
	
	function __construct($db, $post, $smarty){
		$this -> db 	= $db;
		$this -> post 	= $post;
		$this -> smarty = $smarty;
	}
	
	
	function insert(){
		$description = $this -> post['description'];
		$id_category = $this -> post['id_category'];
		if($description != "" && $description != " " && is_numeric($id_category)){
			
			$subcategory = new Subcategory('',$id_category,$description);
		
			$result = $subcategory -> insert($this -> db,$subcategory);
		}
		echo $result;
	}
	
	function update(){
		$id 		 = $this -> post['id'];
		$id_category = $this -> post['id_category'];
		$description = $this -> post['description'];
		
		if($description != "" && $description != " " && is_numeric($id_category)){
			
			$subcategory = new Subcategory($id,$id_category,$description);
		
			$result = $subcategory -> update($this -> db,$subcategory);
		}
		echo $result;
	}
	
	function delete(){
		$id		= $this -> post['id'];
		
		$subcategory = new Subcategory($id,'','');
		
		$result = $subcategory -> delete($this -> db,$subcategory);
		echo $result;
	}
	
	
	function display(){		
		$result = Subcategory::getList($this -> db);
		$list = Category::getList($this -> db);

		$this -> smarty ->assign('listSubcategory' , $result);
		$this -> smarty ->assign('listCategory' , $list);
				
		$this -> smarty ->display('../templates/subcategory/index.tpl');
	}
	
	function main($db,$smarty,$post){
		$method = $post['method'];
		$index = new SubcategoryIndex($db,$post,$smarty);
		
		switch ($method){
			case 'insert':
				$index -> insert();		
				break;
			case 'update':
				$index -> update();		
				break;
			case 'delete':
				$index -> delete();		
				break;
			
			default:
				$index -> display();
		}
	}
}

SubcategoryIndex::main($db,$smarty,$_POST);
?>